package com.sdruixinggroup.mondayb2b.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.models.ShoppingCartBeen;
import com.sdruixinggroup.mondayb2b.widget.MineGoodsView;

import java.util.List;

/**
 * Created by 00029935 on 2017/11/19.
 */

public class ShoppingCartAdapter extends QuickAdapter<ShoppingCartBeen.MerchantsBean> {

    private Context context;
    private  OnDeleteListener onDeleteListener;
    private OnDeleteGoodListener onDeleteGoodListener;
    private OnShopSelectAllorNot onShopSelectAllorNot;

    public ShoppingCartAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getLayoutID(int viewType) {
        return R.layout.shopping_cart_adapter_item;
    }

    @Override
    public void convert(VH holder, final ShoppingCartBeen.MerchantsBean itemData, final int position) {
        final CheckBox  checkBox = holder.getView(R.id.ck_all);
        checkBox.setChecked(itemData.isSelect);



        ImageView ivDelete = holder.getView(R.id.iv_delete);
        if(itemData.isEditabe){
            checkBox.setVisibility(View.INVISIBLE);
            //ivDelete.setVisibility(View.VISIBLE);
        }else {
            checkBox.setVisibility(View.VISIBLE);
            //ivDelete.setVisibility(View.GONE);
        }

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != onDeleteListener){
                    onDeleteListener.listener(itemData,position);
                }
            }
        });
        TextView tvShopName = holder.getView(R.id.tv_shop_name);

        tvShopName.setText(itemData.getName());

       final LinearLayout llGoodsContent = holder.getView(R.id.ll_goods_content);


        final List<ShoppingCartBeen.MerchantsBean.ShoppingCartsBean> been=itemData.getShopping_carts();



        if(null != been && !been.isEmpty()){
            llGoodsContent.removeAllViews();
            for(ShoppingCartBeen.MerchantsBean.ShoppingCartsBean shoppingCartsBean : been){
                MineGoodsView mineGoodsView = new MineGoodsView(context);
                mineGoodsView.setMyCheckedChangeLisener(new MineGoodsView.MyCheckedChangeLisntener() {
                    @Override
                    public void onChecked() {

                        int checkedsize=0;
                        int totalsize=0;
                        for (ShoppingCartBeen.MerchantsBean.ShoppingCartsBean shoppingCartsBean : been){
                            if(shoppingCartsBean.isSelect){
                                checkedsize++;
                            }
                            totalsize++;
                        }
                        if(totalsize==checkedsize) {
                            checkBox.setChecked(true);
                        }
                        if(checkedsize==0){
                            checkBox.setChecked(false);
                        }
                    }
                });

                mineGoodsView.update(shoppingCartsBean);

                llGoodsContent.addView(mineGoodsView);
            }
        }

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



                if(null != been && !been.isEmpty()){
                    llGoodsContent.removeAllViews();
                    for(ShoppingCartBeen.MerchantsBean.ShoppingCartsBean shoppingCartsBean : been){
                        MineGoodsView mineGoodsView = new MineGoodsView(context);
                        mineGoodsView.setMyCheckedChangeLisener(new MineGoodsView.MyCheckedChangeLisntener() {
                            @Override
                            public void onChecked() {

                                int checkedsize=0;
                                int totalsize=0;
                                for (ShoppingCartBeen.MerchantsBean.ShoppingCartsBean shoppingCartsBean : been){
                                    if(shoppingCartsBean.isSelect){
                                        checkedsize++;
                                    }
                                    totalsize++;
                                }
                                if(totalsize==checkedsize) {
                                    checkBox.setChecked(true);
                                }
                                if(checkedsize==0){
                                    checkBox.setChecked(false);
                                }
                            }
                        });
                        shoppingCartsBean.isSelect=isChecked;
                        mineGoodsView.update(shoppingCartsBean);
                        llGoodsContent.addView(mineGoodsView);

                    }
                }

                if(null != onShopSelectAllorNot){
                    onShopSelectAllorNot.listener();
                }
            }
        });

    }

    public   interface OnDeleteListener{
        void listener( ShoppingCartBeen.MerchantsBean itemData, int position);
    }

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public interface OnDeleteGoodListener{
        void deleteGood(ShoppingCartBeen.MerchantsBean.ShoppingCartsBean shoppingCartsBean,int position);
    }

    public void setOnDeleteGoodListener(OnDeleteGoodListener onDeleteGoodListener) {
        this.onDeleteGoodListener = onDeleteGoodListener;
    }


    public interface OnShopSelectAllorNot{
        void listener();
    }

    public void setOnShopSelectAllorNot(OnShopSelectAllorNot onShopSelectAllorNot) {
        this.onShopSelectAllorNot = onShopSelectAllorNot;
    }
}
